package gui;

import java.util.LinkedList;

import javax.swing.table.AbstractTableModel;

import MoC.*;

//////////////////////////////////////////////////////////////////////////
//// MessagesTableModel
/**

@author Ivan Jeukens
@version v 0.1 06/02/2002
*/
public class MessagesTableModel extends AbstractTableModel {

    public MessagesTableModel() {
        super();
        _messages = new LinkedList();
    }

    ///////////////////////////////////////////////////////////////////
    ////                         public methods                    ////

    public void addMessage(Message m) {
        _messages.add(m);
    }
    
    public Message getMessageAt(int row) {
        return (Message) _messages.get(_tableIndex[row]);
    }
    
    public void createIndex() {
        _indexSize = _messages.size();
        _tableIndex = new int[_indexSize];
        for(int i = 0;i < _indexSize;i++) _tableIndex[i] = i;
    }

    public int getColumnCount() {
        return _columnNames.length;
    }

    public String getColumnName(int col) {
        return _columnNames[col];
    }
     
    public int getRowCount() {
        if(_messages == null) return 0;
        return _indexSize;
    }

    public Object getValueAt(int row, int col) {
        Message m = (Message) _messages.get(_tableIndex[row]);
        switch(col) {
            case 0: { 
                return m.getActorName();
            }
            
            case 1: {
                return Message.statusToString(m.getStatus());
            }

            case 2: {
                return Message.phaseToString(m.getPhase());
            }

            case 3: {
                return m.getSummary();
            }

        }
	return "";
    }

    public Class getColumnClass(int col) {
        switch(col) {
	    case 0: 
	    case 1: 
            case 2: 
            case 3: return String.class;
	}
	return null;
    }

    public void maskPhase(String phase) {
        if(phase.equals("All")) {
            _indexSize = _messages.size();
            for(int i = 0;i < _indexSize;i++) _tableIndex[i] = i;
        }
        else {
            _indexSize = 0;
	    for(int i = 0;i < _messages.size();i++) {
                Message m = (Message) _messages.get(i);
	        if(Message.phaseToString(m.getPhase()).equals(phase)) {
	            _tableIndex[_indexSize] = i;
		    _indexSize++;
	        }
	    }
        }
	fireTableDataChanged();
    }

     public void maskMoC(String moc) {
        if(moc.equals("All")) {
            _indexSize = _messages.size();
            for(int i = 0;i < _indexSize;i++) _tableIndex[i] = i;
        }
        else {
            _indexSize = 0;
	    for(int i = 0;i < _messages.size();i++) {
                Message m = (Message) _messages.get(i);
	        if(Message.mocToString(m.getMoC()).equals(moc)) {
	            _tableIndex[_indexSize] = i;
		    _indexSize++;
	        }
	    }
        }
	fireTableDataChanged();
    }
    
    ///////////////////////////////////////////////////////////////////
    ////                         private methods                   //// 

    
    ///////////////////////////////////////////////////////////////////
    ////                         private variables                 ////

    private final String[] _columnNames = { "Actor", "Status", "Phase", "Summary" };

    private LinkedList _messages;

    private int [] _tableIndex;
    private int _indexSize;
}
